package com.ruoyi.web.controller.ykb;

import com.alibaba.fastjson.JSONObject;
import com.digital.szzz.gateway.sdk.api.GatewaySender;
import com.digital.szzz.gateway.sdk.bean.GatewayResponse;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ykb")
@Api(tags = {"渝快办接入API"})
@Slf4j
public class YKBUserController extends BaseController {

	@Value("${ykb.auth.url}")
	private String url;
	@Value("${ykb.auth.appKey}")
	private String appKey;
	@Value("${ykb.auth.appIv}")
	private String appIv;
	@Value("${ykb.auth.appId}")
	private String appId;
	@Value("${ykb.auth.appSecret}")
	private String appSecret;


	@GetMapping("/getJSAPIToken")
	@ApiOperation(value = "渝快办-获取jsapitoken", notes = "获取jsapitoken")
	public AjaxResult getJSAPIToken() {

		// todo 加入缓存
		Object jsapiToken = getToken();
		log.info("返回参数jsapiToken:{}", jsapiToken);
		return success(jsapiToken);
	}

	/**
	 * 获取JSAPIToken
	 *
	 * @return
	 */
	public Object getToken() {
		String url = this.url; //请求地址
		String appId = this.appId;// 应用唯一标识
		String appIv = this.appIv; // app加密偏移量
		String appKey = this.appKey;//pp加密key
		String appSecretKey = this.appSecret;//app加密秘钥
		String authCode = null;// 用户授权令牌传空
		Integer readTimeout = 2000;//接口读取超时时间 ms
		Integer connTimeout = 2000;//接口连接超时时间 ms
		String method = "ykb.app.jsapi.getToken";
		JSONObject parame = new JSONObject();
		parame.put("appId", this.appId);
		Map<String, String> headerMap = new HashMap<>();
		log.info("调用sdk请求参数req url:{},appId:{}", url, appId);
		GatewaySender gatewaySender = new GatewaySender();
		log.info("gatewaySender对象:{}", gatewaySender);
		GatewayResponse send = GatewaySender.send(url, appId, method, appIv, appKey, appSecretKey, parame, authCode, headerMap, readTimeout, connTimeout);
		log.info("调用sdk返回参数res:{}", send);
		return send;
	}

}
